package cn.eeepay.framework.service.impl;

import cn.eeepay.framework.dao.AccountAddressTaskDao;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.AccountAddress;
import cn.eeepay.framework.model.AccountAddressTask;
import cn.eeepay.framework.model.UserLoginInfo;
import cn.eeepay.framework.service.AccountAddressService;
import cn.eeepay.framework.service.AccountAddressTaskService;
import cn.hutool.core.collection.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AccountAddressTaskServiceImpl implements AccountAddressTaskService {

    Logger logger = LoggerFactory.getLogger(AccountAddressTaskServiceImpl.class);

    @Resource
    AccountAddressTaskDao accountAddressTaskDao;
    @Resource
    AccountAddressService accountAddressService;

    @Override
    public int insert(AccountAddressTask accountAddress) {
        return accountAddressTaskDao.insert(accountAddress);
    }

    @Override
    public int update(AccountAddressTask accountAddress) {
        return accountAddressTaskDao.update(accountAddress);
    }

    @Override
    public int delete(Integer id) {
        return accountAddressTaskDao.delete(id);
    }

    @Override
    public List<AccountAddressTask> select(Integer accountAddressId) {
        return accountAddressTaskDao.select(accountAddressId);
    }

    @Transactional
    public void saveOrUpdate(String address,List<AccountAddressTask> list){
        final UserLoginInfo principal = (UserLoginInfo) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();
        if(CollectionUtil.isEmpty(list)){
            accountAddressTaskDao.deleteByAddress(address);
            return ;
        }

        Integer accountAddressId = list.get(0).getAccountAddressId();
        List<AccountAddressTask> currentList = this.select(accountAddressId);
        List<Integer> currentListId = new ArrayList<>();
        for (AccountAddressTask accountAddressTask : currentList) {
            currentListId.add(accountAddressTask.getId());
        }

        List<Integer> newListId = new ArrayList<>();
        for (AccountAddressTask accountAddressTask : list) {
            if(accountAddressTask.getId() == null){             //没有id的则保存
                accountAddressTask.setOperator(principal.getRealName());
                accountAddressTask.setStatus(0);
                accountAddressTask.setCreateTime(new Date());
                this.insert(accountAddressTask);
            }else{
                newListId.add(accountAddressTask.getId());
            }
        }

        currentListId.removeAll(newListId);
        for (Integer id : currentListId) {          //删除前端删除的
            this.delete(id);
        }

        for (AccountAddressTask accountAddressTask : list) {
            if(accountAddressTask.getStatus() == 0){
                accountAddressTask.setOperator(principal.getRealName());
                this.update(accountAddressTask);
            }
        }
    }

    @Override
    public List<AccountAddressTask> selectByAccountAddressId(Integer accountAddressId,Page<AccountAddressTask> page) {
        return accountAddressTaskDao.selectByAccountAddressId(accountAddressId,page);
    }

    @Transactional
    @Override
    public void autoEffective() {
        Date date = new Date();
        List<AccountAddressTask> accountAddressTasks = accountAddressTaskDao.selectByStatusAndDate(date, 0);
        if(CollectionUtil.isNotEmpty(accountAddressTasks)){
            for (AccountAddressTask accountAddressTask : accountAddressTasks) {
                Integer accountAddressId = accountAddressTask.getAccountAddressId();
                AccountAddress accountAddress = accountAddressService.findById(accountAddressId);
                if(accountAddress != null){
                    BigDecimal totalAmount = accountAddressTask.getTotalAmount();
                    accountAddress.setTotalAmount(totalAmount);
                    accountAddressService.update(accountAddress);
                    accountAddressTaskDao.updateStatus(accountAddressTask.getId(),1);
                }
            }
        }
    }

}
